const app = getApp()
import {
  getVIP,
  buyVIP
} from '../../../api/superVip'
import {
  submit,
  query
} from '../../../api/payment';
import {
  list,
  tag
} from '../../../api/article';
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipForm: [{
        name: '体验会员',
        price: 0,
        memo: '新会员专享特权'
      },
      {
        name: '年卡会员',
        price: 0,
        memo: '尊享VIP会员权益'
      },
      {
        name: '永久会员',
        price: 0,
        memo: '一次缴费终生权益'
      },
    ],
    vipType: false,
    vipIndex: 0,
    vipPrice: '0',
    vipName: '元/1月',
    vipData: {},
    timeDate:false,
    questionList: [],
    tagIds: '',
    content: '',
    title: '',
    vipTitle:'体验会员',
    vipSubtitle:'新会员专享特权'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setNavSize()
    this.getVIPClick()
    this.initQuestion()
  },
  setNavSize: function() {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  navTo() {
    let p = getCurrentPages().length;
    if (p <= 1) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  getVIPClick() {
    var _this = this
    getVIP().then(
      res => {
        console.log(res)
        _this.data.vipForm[0].price = (res.data.vip.amount / 12).toFixed(2)
        //_this.data.vipForm[0].memo = res.data.vip.memo
        _this.data.vipForm[1].price = res.data.vip.amount
        //_this.data.vipForm[1].memo = res.data.vip.memo
        _this.data.vipForm[2].price = res.data.vip.quantity
        //_this.data.vipForm[2].memo = res.data.vip.memo
        _this.data.vipPrice = (res.data.vip.amount / 12).toFixed(2)
        _this.data.vipName = '元/1月 立即开通'
        _this.data.vipData = res.data.member
        if (Number(res.data.member.vvip) < Number(res.data.vip.vip)) {
          console.log('1111')
          _this.data.vipType = true
        } else {
          _this.data.vipType = false
          _this.settime(res.data.member.vipExpired)
          console.log('2222')
        }
        _this.setData({
          vipForm: _this.data.vipForm,
          vipType: _this.data.vipType,
          vipPrice: _this.data.vipPrice,
          vipName: _this.data.vipName,
          vipData: _this.data.vipData
        })
      }
    ).catch(error => {
      console.log(error)
    })
  },
  vipClick(data) {
    console.log(data)
    this.data.vipIndex = data.currentTarget.dataset.index
    this.data.vipPrice = this.data.vipForm[data.currentTarget.dataset.index].price
    if (data.currentTarget.dataset.index == 0) {
      this.data.vipName = '元/1月 立即开通'
    } else if (data.currentTarget.dataset.index == 1) {
      this.data.vipName = '元/1年 立即开通'
    } else if (data.currentTarget.dataset.index == 2) {
      this.data.vipName = '元 立即开通'
    }
    this.data.vipTitle = this.data.vipForm[this.data.vipIndex].name
    this.data.vipSubtitle = this.data.vipForm[this.data.vipIndex].memo
    this.setData({
      vipIndex: this.data.vipIndex,
      vipName: this.data.vipName,
      vipPrice: this.data.vipPrice,
      vipTitle:this.data.vipTitle,
      vipSubtitle:this.data.vipSubtitle
    })
  },
  openClick() {
    buyVIP({
      vipType: this.data.vipIndex
    }).then(
      res => {
        console.log(res)
        this.submitClick(res.data.sn)
      }
    ).catch(error => {
      console.log(error)
    })
  },
  submitClick(data) {
    var _this = this
    _this.data.sn = data
    wx.showLoading({
      title: '加载中...',
    })
    submit({
      paymentPluginId: 'weixinMiniPayPlugin',
      sn: data
    }).then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.type == 'success') {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function(red) {
            _this.queryClick()
          },
          'fail': function(red) {
            wx.showToast({
                    icon: "none",
              title: '支付失败',
            })
          }
        })
      } else {
        wx.showToast({
                icon: "none",
          title: '支付失败',
        })
      }
    }).catch(error => {
      wx.showToast({
              icon: "none",
        title: '支付失败',
      })
    })
  },
  queryClick() {
    var _this = this
    query({
      sn: _this.data.sn
    }).then(res => {
      console.log(res)
      if (res.type == 'success') {
        this.getVIPClick()
      } else {
        this.getVIPClick()
      }
    }).catch(error => {
      wx.showToast({
              icon: "none",
        title: error.content,
      })
    })
  },
  settime(diffValue) {
    var _this = this
    if (diffValue==null) {
        diffValue = new Date().getTime+99999999
    }
    var remaining = diffValue - ((new Date()).getTime())
    var time = parseInt(remaining / 1000 / 60 / 60 / 24, 10)
    if(time < 15) {
      _this.data.timeDate = true
      _this.data.vipIndex = 1
      _this.data.vipPrice = _this.data.vipForm[1].price
      _this.data.vipName = '元/续费'
      _this.setData({
        timeDate: _this.data.timeDate,
        vipIndex: _this.data.vipIndex,
        vipPrice: _this.data.vipPrice,
        vipName: _this.data.vipName
      })
    }else{
      _this.data.timeDate = false
      _this.setData({
        timeDate: _this.data.timeDate,
      })
    }
  },
  initQuestion() {
    let params = {
      'mchId': app.globalData.mchId,
      'type': 0,
    }
    tag(params).then(res => {
      console.log(res)
      res.data.data.forEach(item => {
        if (item.name === '会员权益') {
          this.data.tagIds = item.id
          // 查找常见问题
          list({ 'mchId': app.globalData.mchId, 'tagIds': this.data.tagIds }).then(res => {
            console.log(res)
            this.setData({
              questionList: res.data.data
            })
            this.detailsClick()
          }).catch(err => {
            console.log(err)
          })
        }
      })
    }).catch(err => {
      console.log(err)
    })
  },
  detailsClick(){
    var data = {
      mchId: app.globalData.mchId,
      tagIds: this.data.tagIds,
      title: this.data.questionList[0].title
    }
    list(data).then(res => {
      console.log(res)
      let text = res.data.data[0].content
      this.setData({
        content: text,
      })
    }).catch(err => {
      console.log(err)
    })
  }
})